package com.example.demo.anothor;


import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper//将对象交给spring管理
public interface UserInfoMapper {
    //查询所有用户信息

    @Select("select * from user_info")//尽量是不要使用*,要把所有字段都写出来
    List<UserInfo> selectAll();

    @Select("select * from user_info where id = #{id};")
//    @ResultMap("resultMap")
    List<UserInfo> selectById(@Param("id") Integer userId);

    @Select("select * from user_info where username = '${name}';")
    List<UserInfo> selectByName(String name);

    @Select("select * from user_info order by id ${order}")
    List<UserInfo> selectByOrder(String order);

    @Select("select id, username, age, gender, phone, delete_flag, create_time, update_time "
            + "from user_info where username like '%#{key}%' ")
    List<UserInfo> selectByLike(String key);

    @Options(useGeneratedKeys = true, keyProperty = "id")//返回主键
    @Insert("insert into user_info (username, `password`, age, gender, phone)" +
            " values(#{username}, #{password}, #{age}, #{gender}, #{phone})")
    Integer insert( UserInfo userInfo);

    @Insert("insert into user_info (username, `password`, age, gender, phone) " +
            "values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender},#{userInfo.phone})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

    @Delete("Delete from user_info where id = #{id}")
    Integer delete(Integer id);

    @Update("update user_info set username = #{username} where id = #{id}")
    Integer update(UserInfo userInfo);

     @Update("update user_info set username = #{username} where id = #{id}")
    Integer update1(String username, Integer id);

     //起别名
     @Select("select id, username, `password`, age, gender, phone, delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from user_info")
    List<UserInfo> select1();

//     //;结果映射
@Results(id = "resultMap",value = {
        @Result(column = "delete_flag",property = "deleteFlag"),
        @Result(column = "create_time",property = "createTime"),
        @Result(column = "update_time",property = "updateTime")
})
     @Select("select id, username, `password`, age, gender, phone, delete_flag, \n" +
             "create_time, update_time from user_info")
    List<UserInfo> select2();

}
